package com.yonyou.common.service.messagecenter.rabbitmq.demo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.common.service.messagecenter.rabbitmq.MessageProducer;
import com.yonyou.common.service.messagecenter.rabbitmq.MessageProducerUtil;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocol;
import com.yonyou.common.service.messagecenter.util.MqConsts;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping(value = "message")
public class YYDemoController {
	@Autowired
	private MessageProducer producer;
	
	@RequestMapping(value = "/send")
	public @ResponseBody String mqAct() {
		MessageProtocol a=new MessageProtocol();
    	a.setAppid("10001");
    	a.setMessagecontent("{\"code\":\"code1\",\"name\":\"name1\",\"demo1\":\"--demo1\",\"demo2\":\"---demo2--\"}");
    	a.setMessageid("12000");
    	//a.setMessagetype("");
    	a.setOccurtime((new Date().toString()));
    	a.setSign("xxx--sign1-"+UUID.randomUUID().toString());
    	JSONArray jarr=JSONArray.fromObject(a);
    	MessageProtocol message=new MessageProtocol();
    	message.setMessagecontent(jarr.toString());
    	message.setAppid("1");
    	message.setSign(UUID.randomUUID().toString());
		MessageProducerUtil.SendDemoMessage(message);

		Map<String,String> map=new HashMap<String,String>();
			map.put("result", "ok");
			map.put(MqConsts.RESPONSE_DATA_MESSAGE, MqConsts.RESPONSE_DATA_VALUE_SUCCESS_MESSAGE);
		JSONArray jarr1=JSONArray.fromObject(map);
		return jarr1.toString().substring(1, jarr1.toString().length()-1);
	}
	
	@RequestMapping(value = "/send2")
	public @ResponseBody String mqAct2() {
		DemoMessageProtocol2 msg2=new DemoMessageProtocol2();
		msg2.setCode("code1");
		msg2.setName("name1");
		//msg2.setMessagetype("DemoMessageProtocol2");
		JSONArray jarr=JSONArray.fromObject(msg2);
    	MessageProtocol message=new MessageProtocol();
    	message.setMessagecontent(jarr.toString());
    	message.setAppid("1");
    	message.setSign(UUID.randomUUID().toString());
		MessageProducerUtil.SendDemoMessage(message);
		return "ok";
	}
	
	@RequestMapping(value = "/user")
	public @ResponseBody String user(@RequestBody String data) throws UnsupportedEncodingException {
		System.out.println("--------interface data:"+data);

/*		JSONObject obj=JSONObject.fromObject(data);
		String type="DemoMessageProtocol";//MessageProtocol.parseType(data);
		if(type.equals("DemoMessageProtocol2")){
			
		}
		if(type.equals("DemoMessageProtocol")){
			Map<String,Class> map=new HashMap<String,Class>();
	    	map.put("protocol2", DemoMessageProtocol2.class);
	    	//JSONObject obj=JSONObject.fromObject(data);
	    	DemoMessageProtocol jo=(DemoMessageProtocol)JSONObject.toBean(obj, DemoMessageProtocol.class,map);
			System.out.println("--------convert to DemoMessageProtocol:"+jo.getDemo1()+","+
					jo.getDemo2()+",");
		}*/
		Map<String,String> map=new HashMap<String,String>();
			map.put(MqConsts.RESPONSE_DATA_CODE, MqConsts.RESPONSE_DATA_VALUE_SUCCESS);
			map.put(MqConsts.RESPONSE_DATA_MESSAGE, MqConsts.RESPONSE_DATA_VALUE_SUCCESS_MESSAGE);
		JSONArray jarr=JSONArray.fromObject(map);
		return jarr.toString();
	}
}
